package com.nextgenapp.wavetictactoe.controller
{
	import com.nextgenapp.wave.gadget.Wave;
	import com.nextgenapp.wave.gadget.WaveState;
	
	import mx.core.Application;
	
	public class StateController
	{
		private var wave:Wave;
		
		public function StateController(wave:Wave)
		{
			this.wave = wave;
		}

		/**
		 * callback function to receive state callback from wave.  
		 * reset every one of the state according to the 
		 * state stored in wave.
		 */
		public function stateCallback(state:WaveState):void {
			var overallState:String = state.getStringValue("overallState");
			var cellState0:String = state.getStringValue("cellState0");
			var cellState1:String = state.getStringValue("cellState1");
			var cellState2:String = state.getStringValue("cellState2");
			var cellState3:String = state.getStringValue("cellState3");
			var cellState4:String = state.getStringValue("cellState4");
			var cellState5:String = state.getStringValue("cellState5");
			var cellState6:String = state.getStringValue("cellState6");
			var cellState7:String = state.getStringValue("cellState7");
			var cellState8:String = state.getStringValue("cellState8");
			
			if (overallState) {
				Application.application.currentState = overallState;
			}
			
			if (cellState0) { Application.application.cell0.currentState = cellState0; }
			if (cellState1) { Application.application.cell1.currentState = cellState1; }
			if (cellState2) { Application.application.cell2.currentState = cellState2; }
			if (cellState3) { Application.application.cell3.currentState = cellState3; }
			if (cellState4) { Application.application.cell4.currentState = cellState4; }
			if (cellState5) { Application.application.cell5.currentState = cellState5; }
			if (cellState6) { Application.application.cell6.currentState = cellState6; }
			if (cellState7) { Application.application.cell7.currentState = cellState7; }
			if (cellState8) { Application.application.cell8.currentState = cellState8; }
		}
		
		/**
		 * submit all states
		 */
		public function submitState():void {
			// there are better way to implement this without 
			// exposing the global variables like this.  
			// but this is just an example.  for brevity's sake, 
			// we are just doing it this way.  
			var obj:Object = {};
			obj.overallState = Application.application.currentState;
			obj.cellState0 = Application.application.cell0.currentState;
			obj.cellState1 = Application.application.cell1.currentState;
			obj.cellState2 = Application.application.cell2.currentState;
			obj.cellState3 = Application.application.cell3.currentState;
			obj.cellState4 = Application.application.cell4.currentState;
			obj.cellState5 = Application.application.cell5.currentState;
			obj.cellState6 = Application.application.cell6.currentState;
			obj.cellState7 = Application.application.cell7.currentState;
			obj.cellState8 = Application.application.cell8.currentState;
			wave.submitDelta(obj);
		}

	}
}